package com.test.springmvc.crud.handlers;

import com.test.springmvc.crud.dao.DepartmentDao;
import com.test.springmvc.crud.dao.EmployeeDao;
import com.test.springmvc.crud.entities.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2018/10/13 10:59
 */
@Controller
public class EmployeeHandler {

    @Autowired
    private EmployeeDao dao;

    @Autowired
    private DepartmentDao departmentDao;

    @RequestMapping("/emps")
    public String list(Map<String, Object> map) {
        map.put("employees", dao.getEmployees());
        return "list";
    }

    /**
     * 新建用户页面
     *
     * @param map
     * @return
     */
    @RequestMapping(value = "/emp", method = RequestMethod.GET)
    public String input(Map<String, Object> map) {
        map.put("departments", departmentDao.getDepartments());
        map.put("employee", new Employee());
        return "input";
    }

    /**
     * 修改用户界面
     * @param id
     * @param map
     * @return
     */
    @RequestMapping(value = "/emp/{id}", method = RequestMethod.GET)
    public String input(@PathVariable("id") Integer id, Map<String, Object> map) {
        map.put("employee", dao.getEmployee(id));
        map.put("departments", departmentDao.getDepartments());
        return "input";
    }

    /**
     * 如果想得到错误消息，可以在目标方法加上BindingResult参数，从其中获取
     * 是否出现错误以及错误信息内容是什么
     *
     * @param employee
     * @param result
     * @return
     */
    @RequestMapping(value = "/emp", method = RequestMethod.POST)
    public String save(@Valid Employee employee, BindingResult result, Map<String, Object> map) {
        System.out.println(employee);

        if (result.getErrorCount() > 0) {
            System.out.println("出错了");
            for (FieldError error : result.getFieldErrors()) {
                System.out.println(error.getField() + ":" + error.getDefaultMessage());
            }

            //若验证出错，则转向定制的页面
            map.put("departments", departmentDao.getDepartments());
            return "input";
        }

        dao.save(employee);
        return "redirect:/emps";
    }

    /**
     * 删除功能
     * @param id
     * @return
     */
    @RequestMapping(value = "/emp/{id}", method = RequestMethod.DELETE)
    public String del(@PathVariable("id") Integer id) {
        dao.delete(id);
        return "redirect:/emps";
    }

    /**
     * 配合修改页面，用于信息回显
     * @param id
     * @param map
     */
    @ModelAttribute
    public void getEmployee(@RequestParam(value = "id", required = false) Integer id, Map<String, Object> map) {
        if (id != null) {
            map.put("employee", dao.getEmployee(id));
        }
    }

    /**
     * 更新信息
     * @param employee
     * @return
     */
    @RequestMapping(value = "/emp", method = RequestMethod.PUT)
    public String update(Employee employee) {
        dao.save(employee);
        return "redirect:/emps";
    }

    /*@InitBinder
    public void initBinder(WebDataBinder binder){
        binder.setDisallowedFields("lastName");
    }*/
}
